﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TR.AppService.Dto.WHOMDto;
using TR.Domain.Entity;
using TR.Domain.Entity.WH_return_;
using TR.Domain.Entity.WHOB;
using TR.Domain.Repository.WHOM_return_suppy;

namespace TR.EFCore.Repository.WHOM_return_suppyEFcode
{
    public class wh_return_suppy_E : wh_return_suppy_Dom
    {
        private readonly MyContext context;
        private int fyzts;
        public wh_return_suppy_E(MyContext context)
        {
            this.context = context;
        }
        

          async Task<List<wh_return_suppy_h>> wh_return_suppy_Dom.WHRCZ(int dqy, int mys, string? qsj, string? hsj)
        {
            //声明linq语法
            var query = context.wh_return_suppy_h.AsQueryable();
           

            if (!string.IsNullOrEmpty(qsj) && !string.IsNullOrEmpty(hsj))
            {
                query = query.Where(p => string.Compare(p.sr_date, qsj) >= 0
                               && string.Compare(p.sr_date, hsj) <= 0);
            }

            ////根据条件查询 进货单号、单据状态、供应商
            //if (ws != null)
            //{
            //    //单据编号
            //    if (!string.IsNullOrEmpty(ws.buy_code))
            //    {
            //        query = query.Where(p => EF.Functions.Like(p.buy_code, "%" + ws.buy_code + "%"));
            //    }
                
            //    if (!string.IsNullOrEmpty(ws.supply_code))
            //    {
            //        query = query.Where(p => p.supply_code == ws.supply_code);
            //    }
            //}
            var result = await query.OrderByDescending(p => p.create_time).Skip(dqy * (mys - 1)).Take(dqy).ToListAsync();//异步使用ToListAsync
            fyzts = await query.CountAsync(); // 获取符合查询条件的记录数
            return result;
       
    }
        public async Task<int> WHRShu()
        {
            return  fyzts;
        }

        public async Task<List<wh_orderbuy_d>> Regret(string buy_code)
        {
            // 使用LINQ查询从数据库中获取wh_orderbuy_d数据集合
            var wh_Orderbuy_Ds = await Task.Run(() =>
            {
                return context.wh_orderbuy_d
                              .Where(o => o.buy_code == buy_code)
                              .ToList();
            });

            return wh_Orderbuy_Ds;
        }
        //新增进货退货
        public async Task<bool> addwhom_h(wh_return_suppy_h whReturnSuppyH)
        {
            try
            {
                await context.wh_return_suppy_h.AddAsync(whReturnSuppyH);
                await context.SaveChangesAsync();
                return true; // 添加成功
            }
            catch (Exception ex)
            {
                // 处理异常，或者根据需要返回适当的错误信息
                Console.WriteLine($"Error adding entity to database: {ex.Message}");
                return false; // 添加失败
            }
        }
        //新增退货明细
        public async Task<bool> addwhom_d(wh_return_suppy_d wh_Return_Suppy_D)
        {
            try
            {
                await context.wh_return_suppy_d.AddAsync(wh_Return_Suppy_D);
                await context.SaveChangesAsync();
                return true;
            }
            catch (Exception ex) 
            {
                // 处理异常，或者根据需要返回适当的错误信息
                Console.WriteLine($"Error adding entity to database: {ex.Message}");
                return false; // 添加失败
            }
        }
    }
}
